﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MP3Scrambler
{
    public partial class MainForm : Form
    {
        private MP3Files mp3Files;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.mp3Files = new MP3Files();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.filePathTextBox.Text = folderBrowserDialog.SelectedPath;
                this.pathLoaded();
            }
        }

        private void filePathTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if(Directory.Exists(this.filePathTextBox.Text))
                {
                    e.SuppressKeyPress = true;
                    this.pathLoaded();
                }
            }
        }

        private void pathLoaded()
        {
            this.fileDataGrid.Rows.Clear();

            object[,] fileData;
            if (Directory.Exists(this.filePathTextBox.Text))
            {
                this.mp3Files.setDirectory(this.filePathTextBox.Text);
                fileData = this.mp3Files.getFileData();
            }
            else
            {
                fileData = new object[0,0];
            }
            
            
            for (int i = 0; i < fileData.GetLength(0); i++)
            {
                string[] s = { (string)fileData[i, 1], (string)fileData[i, 2], fileData[i, 3].ToString(), fileData[i,4].ToString()};
                this.fileDataGrid.Rows.Add(s);
            }

            this.fileDataGrid.AutoResizeColumns();
            int totalW = fileName.Width + newFileName.Width;
            fileName.Width = totalW / 2;
            newFileName.Width = totalW / 2;

        }

        private void renameButton_Click(object sender, EventArgs e)
        {
            renameButton.Enabled = false;
            statusLabel.Text = "Processing files...";
            backgroundWorker.RunWorkerAsync();
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            renameProgress.Value = e.ProgressPercentage;
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker w = sender as BackgroundWorker;
            e.Result = this.mp3Files.reOrderFiles(w);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            renameButton.Enabled = true;

            if ((bool)e.Result)
            {
                renameProgress.Value = 100;
                statusLabel.Text = "Files renamed and redated :D";
            }
            else
            {
                renameProgress.Value = 0;
                statusLabel.Text = "Files not changed :(";
            }

            this.pathLoaded();
        }
    }
}
